package com.bawei.movie.viewmodel

import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.bawei.common.entity.MovieVideoEntity
import com.bawei.common.vm.BaseViewModel
import com.bawei.movie.intent.MovieVIewIntent
import com.bawei.movie.model.MoviePagingSource
import com.bawei.movie.model.MovieRepository
import com.bawei.movie.uistate.MovieUiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * Author: Jason Zhang
 * Date: 2024/3/5
 * Desc:
 */
@HiltViewModel
class MovieViewModel @Inject constructor(private val repository: MovieRepository) : BaseViewModel<MovieVIewIntent>() {

    private val _uiState = MutableStateFlow<MovieUiState>(MovieUiState.LoadEmpty)
    val uiState: StateFlow<MovieUiState> = _uiState.asStateFlow()

    override fun dispatchIntent(intent: MovieVIewIntent) {
        when (intent) {
            is MovieVIewIntent.LoadLeverOneType -> loadLeverOneType()
            is MovieVIewIntent.LoadLeverTwoType -> loadLeverTwoType(intent.pid)
        }
    }

    private fun loadLeverOneType() {
        viewModelScope.launch {
            try {
                val (code, data, msg) = repository.getMutilType()
                if (code == 0 && data.isNotEmpty()) {
                    _uiState.value = MovieUiState.LoadLeverOneTypeSuccess(data)
                } else {
                    _uiState.value = MovieUiState.LoadLeverOneTypeFail(msg)
                }
            } catch (e: Exception) {
                _uiState.value = MovieUiState.LoadLeverOneTypeFail("网络请求失败！")
            } finally {
                this.cancel()
            }
        }
    }

    private fun loadLeverTwoType(pid: Int) {
        viewModelScope.launch {
            try {
                val (code, data, msg) = repository.getMutilTypeById(pid)
                if (code == 0 && data.isNotEmpty()) {
                    _uiState.value = MovieUiState.LoadLeverTwoTypeSuccess(data)
                } else {
                    _uiState.value = MovieUiState.LoadLeverTwoTypeFail(msg)
                }
            } catch (e: Exception) {
                _uiState.value = MovieUiState.LoadLeverTwoTypeFail("网络请求失败！")
            } finally {
                this.cancel()
            }
        }

    }

    /**
     * 获取推荐视频
     */
    fun loadRecommendVideo(): Flow<PagingData<MovieVideoEntity>> {
        return Pager(config = PagingConfig(
            initialLoadSize = 10, //设置默认记载数据大小
            pageSize = 10, //每页加载数据大小
            enablePlaceholders = false //无数据时展示空白布局
        ), pagingSourceFactory = { MoviePagingSource() }
        ).flow.cachedIn(viewModelScope)
    }

    /**
     * 获取分类视频
     */
    fun loadMutilVideoById(typeId: Int): Flow<PagingData<MovieVideoEntity>> {
        return Pager(config = PagingConfig(
            initialLoadSize = 10, //设置默认记载数据大小
            pageSize = 10, //每页加载数据大小
            enablePlaceholders = false //无数据时展示空白布局
        ), pagingSourceFactory = { MoviePagingSource(typeId) }
        ).flow.cachedIn(viewModelScope)
    }


}